package com.zhongxi.guoyuan.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.okhttp.Request;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.http.OkHttpClientManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class BaseActivity extends AppCompatActivity {

    protected Context context;

    //dialogFlag = 1, 2, 3, 4;
    //1为普通的网络加载请求动画，2为内嵌的网络加载动画，3为没有任何网络加载动画，4为有1的网络加载动画，但是成功后没有结束动画
    //5为没有网络加载动画，但是有1的结束动画，6,为有2的网络加载动画，但是成功后没有结束动画，7为没有2的网络加载动画，但是有2的结束动画
    public int pageNum = 1;

    //网络请求
    private ProgressDialog progressDialog;

    protected TextView tvTitle;
    protected ImageView ivBack;

    class StringCallback extends OkHttpClientManager.ResultCallback<String> {
        @Override
        public void onResponse(String response) {
            Log.i("base", response);
            try {
                JSONObject obj = new JSONObject(response);
                    int code = obj.getInt("code");
                    if(code == 1){
                        processSuccessResult(response);
                    }else if(code == 7002){
                        processOldUser(response);
                    }
                    else{
                        ProcessFailResult(response);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }finally {
                    hideProgressDialog();
                }
        }

        @Override
        public void onError(Request request, Exception e) {
            processFailRequest();
        }
    }

    public void processOldUser(String response) {

    }

    /**
     * 隐藏对话框
     */
    private void hideProgressDialog() {
        if(progressDialog != null && progressDialog.isShowing()){
            progressDialog.dismiss();
            progressDialog.cancel();
        }
    }

    private void processFailRequest() {
    }

    /**
     * 弹出提示
     * @param message
     */
    protected void showShortToast(String message) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }


    /**
     * 展示提交或者加载数据的进度框
     * @param method
     */
    private void showProgressDialog(String method) {
        try {
            if (progressDialog == null)
                progressDialog = new ProgressDialog(this, ProgressDialog.THEME_HOLO_LIGHT);
            progressDialog.setCancelable(true);
            String progressDialogMessage = "";
            if (!progressDialog.isShowing()) {
                if (method.equals("get"))
                    progressDialogMessage = getResources().getString(R.string.get);
                else if (method.equals("upload"))
                    progressDialogMessage = getResources().getString(R.string.upload);
                else
                    progressDialogMessage = "正在请求数据……";
                progressDialog.setMessage(progressDialogMessage);
                progressDialog.show();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    protected void ProcessFailResult(String response) {
    }

    protected void processSuccessResult(String response) {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        initView();
        initData();
        initEvent();
    }

    protected void initTitle(){
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        ivBack = (ImageView) findViewById(R.id.ivBack);
    }

    /*protected void initList(){
        if(mPullToRefreshListView != null){
            mPullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
            startLabels = mPullToRefreshListView.getLoadingLayoutProxy(true, false);
            startLabels.setPullLabel("下拉刷新");//刷新时
            startLabels.setReleaseLabel("释放刷新");//下来达到一定距离时，显示的提示
            endLabels = mPullToRefreshListView.getLoadingLayoutProxy(false, true);
            endLabels.setPullLabel("加载更多");//刚下拉时，显示的提示
            endLabels.setReleaseLabel("释放加载");//下拉达到一定距离时，显示的提示
            endLabels.setRefreshingLabel("正在加载……");//刷新时
        }
    }*/

    /*protected void initLoading() {
        ll_all = (LinearLayout) findViewById(R.id.ll_all);
        loading = findViewById(R.id.v_loading);
        noNetwork = findViewById(R.id.v_no_network);
        bt_reload = (Button) findViewById(R.id.bt_reload);
    }*/

    protected void initEvent() {
    }

    protected void initData() {
    }

    protected void initView() {
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        if(progressDialog != null) {
            progressDialog.dismiss();
            progressDialog.cancel();
        }
        super.onDestroy();
    }

    /**
     * 获取网络请求 map -- 传递参数
     * @param
     */
    public void getServer(String url, Map<String, String> map,  String getStr, boolean isUser, StringCallback callback, String type){
        try {
            String baseUrl = "";
            OkHttpClientManager okHttp = OkHttpClientManager.getInstance();
            if (map == null) {
                map = new HashMap<String, String>();
            }
            if(callback == null){
                callback = new StringCallback();
            }
            baseUrl = url + MyApplication.getInstance().getCheck(isUser);
            Log.i("base", baseUrl);
            if ("get".equals(type)) {
                okHttp.getAsyn(baseUrl+getStr, callback);
            } else if("post".equals(type)){
                okHttp.postAsyn(baseUrl+getStr, callback, map);
            }
            showProgressDialog(type);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void uploadServer(String url,boolean isUser, StringCallback callback, String key, File content){
        try {
            String baseUrl = "";
            OkHttpClientManager okHttp = OkHttpClientManager.getInstance();
            if(callback == null){
                callback = new StringCallback();
            }
            showProgressDialog("post");
            baseUrl = url + MyApplication.getInstance().getCheck(isUser);
            Log.i("base", baseUrl);
            okHttp.postAsyn(baseUrl, callback, content, key);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 页面跳转
     * @param clazz
     */
    public void startAct(Class clazz){
        Intent intent = new Intent(context, clazz);
        startActivity(intent);
    }

}
